#!/bin/bash - 
#
#  Usage:     Invoking 'bc' with 'here document'.
#  Author:    bucunzai <freecityer@gmail.com>
#  Version:   1.0
#  Created:   03/09/2011 03:16:46 PM CST
#

set -o nounset                              # Treat unset variables as an error

var1=`bc << EOF
18.33 * 19.78
EOF
`
echo $var1

# $( ... ) notation also works
v1=23.53
v2=17.881
v3=83.501
v4=171.63

var2=$(bc << EOF
scale=4
a = ( $v1 + $v2 )
b = ( $v3 * $v4 )
a * b + 15.35
EOF
)
echo $var2

var3=$(bc -l << EOF
scale=9
s ( 1.7 )
EOF
)

echo $var3

# Now, try it in a function...
hypotenuse () 
{
hyp=$(bc -l << EOF
scale=9
sqrt ( $1 * $1 + $2 * $2 )
EOF
)
echo "$hyp"
}

hyp=$(hypotenuse 3.68 7.31)
echo "hypotenuse = $hyp"
exit 0
